package cn.schoolwow.util.test.module.compare.instanceList.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.beanCopy.BeanCopy;
import cn.schoolwow.util.domain.compare.instanceList.UpdateInstanceListCompare;
import cn.schoolwow.util.test.module.compare.instanceList.domain.AddProjectTimeNode;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class InitialCompareInstanceListDataFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setBeforeInstanceList(flowContext);
        setAfterInstanceList(flowContext);
        setAddCompareList(flowContext);
        setUpdateInstanceListCompare(flowContext);
        setDeleteCompareList(flowContext);
    }

    @Override
    public String name() {
        return "初始化比较实例列表参数";
    }

    private void setBeforeInstanceList(FlowContext flowContext){
        List<AddProjectTimeNode> beforeInstanceList = new ArrayList<>();
        for(int i=0;i<4;i++){
            AddProjectTimeNode addProjectTimeNode = new AddProjectTimeNode();
            addProjectTimeNode.name = "时间节点"+i;
            addProjectTimeNode.deadline = LocalDate.now().plusMonths(i);
            beforeInstanceList.add(addProjectTimeNode);
        }
        flowContext.putTemporaryData("beforeInstanceList", beforeInstanceList);
    }

    private void setAfterInstanceList(FlowContext flowContext) throws IOException, ClassNotFoundException {
        List<AddProjectTimeNode> beforeInstanceList = (List<AddProjectTimeNode>) flowContext.checkData("beforeInstanceList");

        List<AddProjectTimeNode> afterInstanceList = new ArrayList<>();
        List<AddProjectTimeNode> cloneList = (List<AddProjectTimeNode>) BeanCopy.deepCloneObject(beforeInstanceList);
        afterInstanceList.addAll(cloneList);

        flowContext.putTemporaryData("afterInstanceList", afterInstanceList);
    }

    private void setAddCompareList(FlowContext flowContext){
        List<AddProjectTimeNode> afterInstanceList = (List<AddProjectTimeNode>) flowContext.checkData("afterInstanceList");

        List<AddProjectTimeNode> addCompareList = new ArrayList<>();

        AddProjectTimeNode addProjectTimeNode = new AddProjectTimeNode();
        addProjectTimeNode.name = "时间节点5";
        addProjectTimeNode.deadline = LocalDate.now().plusMonths(12);
        afterInstanceList.add(addProjectTimeNode);

        addCompareList.add(addProjectTimeNode);

        flowContext.putTemporaryData("addCompareList", addCompareList);
    }

    private void setUpdateInstanceListCompare(FlowContext flowContext){
        List<UpdateInstanceListCompare> updateInstanceListCompareList = new ArrayList<>();

        List<AddProjectTimeNode> beforeInstanceList = (List<AddProjectTimeNode>) flowContext.checkData("beforeInstanceList");
        List<AddProjectTimeNode> afterInstanceList = (List<AddProjectTimeNode>) flowContext.checkData("afterInstanceList");

        for(int i=0;i<2;i++){
            AddProjectTimeNode addProjectTimeNode = afterInstanceList.get(i);
            addProjectTimeNode.deadline = LocalDate.now().plusMonths(10+i);

            UpdateInstanceListCompare updateInstanceListCompare = new UpdateInstanceListCompare();
            updateInstanceListCompare.beforeInstance = beforeInstanceList.get(i);
            updateInstanceListCompare.afterInstance = afterInstanceList.get(i);
            updateInstanceListCompareList.add(updateInstanceListCompare);
        }
        flowContext.putTemporaryData("updateInstanceListCompareList", updateInstanceListCompareList);
    }

    private void setDeleteCompareList(FlowContext flowContext){
        List<AddProjectTimeNode> afterInstanceList = (List<AddProjectTimeNode>) flowContext.checkData("afterInstanceList");

        List<AddProjectTimeNode> deleteCompareList = new ArrayList<>();
        AddProjectTimeNode removeAddProjectTimeNode = afterInstanceList.remove(2);
        deleteCompareList.add(removeAddProjectTimeNode);

        flowContext.putTemporaryData("deleteCompareList", deleteCompareList);
    }

}
